package com.cjh.web;

import com.cjh.pojo.Cart;
import com.cjh.pojo.User;
import com.cjh.service.OrderService;
import com.cjh.service.impl.OrderServiceImpl;
import com.cjh.utils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: huahua
 * @Date: 2020-09-17 22:46
 */
public class OrderServlet extends BaseServlet {


    private OrderService orderService = new OrderServiceImpl();
    /**
     * 生成订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //获取userId
        User user = (User) req.getSession().getAttribute("user");
        if (user==null){
            req.getRequestDispatcher("pages/user/login.jsp").forward(req,resp);
            return;
        }

        System.out.println("orderServlet程序在"+Thread.currentThread().getName()+"中");
        Integer id = user.getId();
        //调用方法
        String orderId = orderService.createOrder(cart, id);

//          请求转发的方法
//        req.setAttribute("orderId",orderId);
//        req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);

        //重定向的方法
        req.getSession().setAttribute("orderId",orderId);
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");
    }
}
